<?php
declare(strict_types=1);

namespace app\api\controller;

use app\basic\ApiController;
use app\api\logic\OrderLogic;
use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("订单接口")
 */
class OrderController extends ApiController
{
    protected $logic;

    public function __construct()
    {
        $this->logic = new OrderLogic();
        parent::__construct();
    }

     /**
     * @Apidoc\Title("获取收银台信息")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("goods_id", type="int", require=true, desc="商品ID")
     * @Apidoc\Param("total_num", type="int", require=true, desc="购买数量")
     * @Apidoc\Param("pay_type", type="int", require=true, desc="支付方式:1=微信 2=支付宝")
     * @Apidoc\Param("client_type", type="int", default="1", desc="客户端类型")
     * @Apidoc\Param("coupons_id", type="int", default="0", desc="优惠券ID")
     * @Apidoc\Returned("data", type="object", desc="订单信息", children={
     *   @Apidoc\Returned("order_id", type="int", desc="订单ID"),
     *   @Apidoc\Returned("order_no", type="string", desc="订单号"),
     *   @Apidoc\Returned("pay_price", type="string", desc="支付金额")
     * })
     */
    public function getCreate()
    {
        $params = $this->request->only([
            'goods_id',
            'total_num',
        ]);
        if (!$result = $this->logic->getCreateOrderInfo($this->auth->getUserId(), $params)) {
            return $this->fail($this->logic->getError());
        }
        return $this->success($result);
    }

    /**
     * @Apidoc\Title("创建订单")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("goods_id", type="int", require=true, desc="商品ID")
     * @Apidoc\Param("total_num", type="int", require=true, desc="购买数量")
     * @Apidoc\Param("pay_type", type="int", require=true, desc="支付方式:1=微信 2=支付宝")
     * @Apidoc\Param("client_type", type="int", default="1", desc="客户端类型")
     * @Apidoc\Param("coupons_id", type="int", default="0", desc="优惠券ID")
     * @Apidoc\Returned("data", type="object", desc="订单信息", children={
     *   @Apidoc\Returned("order_id", type="int", desc="订单ID"),
     *   @Apidoc\Returned("order_no", type="string", desc="订单号"),
     *   @Apidoc\Returned("pay_price", type="string", desc="支付金额")
     * })
     */
    public function create()
    {
        $params = $this->request->only([
            'goods_id',
            'total_num',
            'pay_type',
            'client_type',
            'coupons_id'
        ]);
        if (!$result = $this->logic->createOrder($this->auth->getUserId(), $params)) {
            return $this->fail($this->logic->getError());
        }
        return $this->success($result);
    }

    /**
     * @Apidoc\Title("创建会员卡订单")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("card_id", type="int", require=true, desc="会员卡ID")
     * @Apidoc\Param("pay_type", type="int", require=true, desc="支付方式：1=余额支付 2=微信支付 3=支付宝支付")
     * @Apidoc\Param("client_type", type="int", require=true, desc="客户端类型：1=PC 2=H5 3=小程序 4=APP")
     * @Apidoc\Param("coupons_id", type="int", require=false, desc="优惠券ID")
     */
    public function createVipOrder()
    {
        if (!$this->auth->isLogin()) {
            return $this->fail('请先登录');
        }
        $params = $this->request->only([
            'card_id',
            'pay_type',
            'client_type',
            'coupons_id'
        ]);

        $result = $this->logic->createVipOrder(
            $this->auth->getUserId(),
            $params
        );

        if (!$result) {
            return $this->fail($this->logic->getError());
        }

        return $this->success($result);
    }

      /**
     * @Apidoc\Title("获取购买记录")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("page", type="int", default="1", desc="页码")
     * @Apidoc\Param("limit", type="int", default="10", desc="每页数量")
     * @Apidoc\Returned("data", type="object", desc="返回数据", children={
     *     @Apidoc\Returned("total", type="int", desc="总记录数"),
     *     @Apidoc\Returned("list", type="array", desc="记录列表", children={
     *         @Apidoc\Returned("order_id", type="int", desc="订单ID"),
     *         @Apidoc\Returned("order_no", type="string", desc="订单号"),
     *         @Apidoc\Returned("title", type="string", desc="文档标题"),
     *         @Apidoc\Returned("pay_price", type="string", desc="支付金额"),
     *         @Apidoc\Returned("pay_time", type="string", desc="支付时间"),
     *         @Apidoc\Returned("file_size", type="string", desc="文件大小"),
     *         @Apidoc\Returned("file_type", type="string", desc="文件类型"),
     *         @Apidoc\Returned("status", type="int", desc="订单状态：10=待支付 20=已支付")
     *     })
     * })
     */
    public function getPurchaseList()
    {
        if (!$this->auth->isLogin()) {
            return $this->fail('请先登录');
        }
        $params = $this->request->only(['page', 'limit']);
        $result = $this->logic->getPurchaseList($this->auth->getUserId(), $params);
        if ($result === false) {
            return $this->fail($this->logic->getError());
        }
        return $this->success($result);
    }

     /**
     * @Apidoc\Title("获取订单统计")
     * @Apidoc\Method("POST")
     * @Apidoc\Returned("data", type="object", desc="返回数据", children={
     *     @Apidoc\Returned("total_amount", type="string", desc="总消费金额"),
     *     @Apidoc\Returned("total_orders", type="int", desc="总订单数")
     * })
     */
    public function getOrderStats()
    {
        if (!$this->auth->isLogin()) {
            return $this->fail('请先登录');
        }
        $result = $this->logic->getOrderStats($this->auth->getUserId());
        if ($result === false) {
            return $this->fail($this->logic->getError());
        }

        return $this->success($result);
    }


}